//
//  NSString+FKAdd.m
//  FKNSURLSessionEncapsulation
//
//  Created by 云眼科技 on 16/7/8.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "NSString+FKAdd.h"

@implementation NSString (FKAdd)

+(NSString *)fk_formatSpeed:(long long)downloadSpeed{
    return [NSString stringWithFormat:@"下载速度:%.2f%@bps", [self calculateFileSizeInUnit:downloadSpeed], [self calculateUnit:downloadSpeed]];
}

+(NSString *)fk_formatLeftTime:(NSInteger)leftTime{
    
    NSString *formatString = nil;
    
    NSInteger hour = leftTime / 3600;
    
    
    if (hour > 1) {
        formatString = @"more than one hour";
    }else{
        NSInteger min = leftTime / 60;
        NSInteger sec = leftTime % 60;
        if (min >=1) {
            formatString = [NSString stringWithFormat:@"%02ld:%02ld", min, sec];
        }else{
            formatString = [NSString stringWithFormat:@"%ld Sec", leftTime];
        }
    }
    return [NSString stringWithFormat:@"剩余时间:%@", formatString];
}

+(NSString *)fk_formatFileSize:(long long)allFileSize currentFileSize:(long long)currentSize progress:(float)progress{
    NSString *haveDone = [NSString stringWithFormat:@"%.2f%@", [self calculateFileSizeInUnit:currentSize], [self calculateUnit:currentSize]];
    return [NSString stringWithFormat:@"文件大小:%.2fM, 已下载:%@(%.f%%)", [self calculateFileSizeInUnit:allFileSize], haveDone, progress * 100];
}




+(float)calculateFileSizeInUnit:(long long)contentLength{
    if(contentLength >= pow(1024, 3)){
        return (float) (contentLength / (float)pow(1024, 3));
    }else if(contentLength >= pow(1024, 2)){
        return (float) (contentLength / (float)pow(1024, 2));
    }else if(contentLength >= 1024){
        return (float) (contentLength / (float)1024);
    }else{
        return (float) (contentLength);
    }
}


+(NSString *)calculateUnit:(long long)contentLength{
    if(contentLength >= pow(1024, 3)){
        return @"G";
    }else if(contentLength >= pow(1024, 2)){
        return @"M";
    }else if(contentLength >= 1024){
        return @"K";
    }else{
        return @"b";
    }
}


@end
